//
//  ZZCDetailViewController.m
//  B段项目
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "ZZCDetailViewController.h"
#import "ZZCDynamicImageCell.h"
#import "ZZCDynamicTableViewCell.h"
#import "ZZCListViewController.h"
#import "TOTools.h"
@interface ZZCDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain)UITableView *fragListTableView;
@property (nonatomic, retain)NSMutableArray *fragDetailArray;
@property (nonatomic, assign)BOOL isUpLoad;
@property (nonatomic, assign)NSInteger nextPage;
@property (nonatomic, retain)NSMutableArray *addTimeArray;
@property (nonatomic, assign)CGFloat sizeHeight;

@end

@implementation ZZCDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //图标
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - WIDTH/4.5, WIDTH/40, WIDTH/16, WIDTH/16)];
    img.image = [UIImage imageNamed:@"iconfont-siyecao123.png"];
    img.center = CGPointMake(WIDTH - WIDTH/5, self.navigationController.navigationBar.frame.size.height /2);
    [self.rightView addSubview:img];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.titleLabel.text = self.nameStr;
    
    self.fragListTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.fragListTableView.delegate = self;
    self.fragListTableView.dataSource = self;
    [self.view addSubview:self.fragListTableView];
    
    [self.fragListTableView registerClass:[ZZCDynamicTableViewCell class] forCellReuseIdentifier:@"DetailCell"];
    [self.fragListTableView registerClass:[ZZCDynamicImageCell class] forCellReuseIdentifier:@"ImageCell"];
    
    self.fragDetailArray = [NSMutableArray array];
    self.addTimeArray = [NSMutableArray array];

    self.isUpLoad = NO;
    
    MJRefreshGifHeader *header = [TOTools createTableViewGifHeaderWithTarget:self Action:@selector(addHeaderRefresh)];
    self.fragListTableView.mj_header = header;
    [self.fragListTableView.mj_header beginRefreshing];
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addFooterRefresh)];
    self.fragListTableView.mj_footer = footer;
}

#pragma mark -- 设置下拉刷新
-(void)addHeaderRefresh
{
    self.isUpLoad = NO;//标记为下拉操作
    // 重新请求数据
    [self getAFNetWorkingData];
    
}



#pragma mark -- 设置上拉刷新
-(void)addFooterRefresh
{
    self.nextPage += 10;
    self.isUpLoad = YES;
    // 重新请求数据
    [self getAFNetWorkingDataUpLoad];
}


#pragma mark- 数据解析
- (void)getAFNetWorkingData{
    
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hub show:YES];
    hub.labelText = @"正在为您加载.....";
    [self.view addSubview:hub];
    
    NSString *urlStr = @"http://api2.pianke.me/timeline/list";
    NSString *para = [NSString stringWithFormat:@"auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&limit=10&start=0&tag=%@&version=3.0.6", self.nameStr];
    [ZYTools postUrl:urlStr body:para result:ZYJSON requestStyle:ZYRequestString headerFile:nil success:^(id result) {
        if (self.isUpLoad == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.fragDetailArray removeAllObjects];
        }
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        NSMutableArray *array = [bigDic valueForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            ZZCDynamicDetailModel *fragDetail = [[ZZCDynamicDetailModel alloc] init];
            [fragDetail setValuesForKeysWithDictionary:dic];
            [self.fragDetailArray addObject:fragDetail];
            [self.addTimeArray addObject:[dic objectForKey:@"addtime"]];
        }
        [self.fragListTableView.mj_header endRefreshing];
        [self.fragListTableView reloadData];
        
        [hub hide:YES];
    } failure:^(NSError *error) {
        
        [hub hide:YES];
    }];
}

#pragma mark - 上拉
- (void)getAFNetWorkingDataUpLoad{
    NSString *urlStr = @"http://api2.pianke.me/timeline/list";
    NSString *para = [NSString stringWithFormat:@"addtime=%@&auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&limit=10&start=%ld&tag=%@&version=3.0.6", self.addTimeArray.lastObject, (long)self.nextPage, self.nameStr];
    [ZYTools postUrl:urlStr body:para result:ZYJSON requestStyle:ZYRequestString headerFile:nil success:^(id result) {
        
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        NSMutableArray *array = [bigDic valueForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            ZZCDynamicDetailModel *fragDetail = [[ZZCDynamicDetailModel alloc] init];
            [fragDetail setValuesForKeysWithDictionary:dic];
            [self.fragDetailArray addObject:fragDetail];
            [self.addTimeArray addObject:[dic objectForKey:@"addtime"]];
        }
        
        [self.fragListTableView.mj_footer endRefreshing];
        [self.fragListTableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
    
}



#pragma mark - cell数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.fragDetailArray.count;
}



#pragma mark - 设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *fragCellIdntifier = @"DetailCell";
    ZZCDynamicTableViewCell *fragListCell = [tableView dequeueReusableCellWithIdentifier:fragCellIdntifier];
    static NSString *fragImageCellIdntifier = @"ImageCell";
    ZZCDynamicImageCell *fragImageCell = [tableView dequeueReusableCellWithIdentifier:fragImageCellIdntifier];
    // 取消点击cell的效果(在设置cell里写)
    fragListCell.selectionStyle = UITableViewCellSelectionStyleNone;
    fragImageCell.selectionStyle = UITableViewCellSelectionStyleNone;
    ZZCDynamicDetailModel *fragDetail = [self.fragDetailArray objectAtIndex:indexPath.item];
    
    if ([fragDetail.coverimg_wh isEqualToString:@""]) {
        fragListCell.dynDetailList = fragDetail;
        
        self.sizeHeight = [ZZCDynamicTableViewCell heightForContent:fragListCell.dynDetailList.content] + fragListCell.markImageView.frame.origin.y + fragListCell.markImageView.frame.size.height +HEIGHT / 25;
        
        return fragListCell;
    } else{
        fragImageCell.dynamicDetailImage = fragDetail;
        
        self.sizeHeight = [ZZCDynamicImageCell heightForContent:fragImageCell.dynamicDetailImage.content] + fragImageCell.markImageView.frame.origin.y + fragImageCell.markImageView.frame.size.height + HEIGHT / 25;
        
        return fragImageCell;
    }
}



#pragma mark - 触摸cell方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZZCListViewController *listDetailVC = [[ZZCListViewController alloc] init];
    ZZCDynamicDetailModel *fragDetailModel = [self.fragDetailArray objectAtIndex:indexPath.row];
    listDetailVC.contentId = fragDetailModel.contentid;
    [self.navigationController pushViewController:listDetailVC animated:YES];
    
}



#pragma mark - cell高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZZCDynamicDetailModel *fragDetail = [self.fragDetailArray objectAtIndex:indexPath.row];
    if ([fragDetail.coverimg_wh isEqualToString:@""]) {
        return self.sizeHeight;
    } else {
        return self.sizeHeight;
    }
}


    

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
